using Microsoft.EntityFrameworkCore;
using UniversalAdmin.Domain.Entities;
using UniversalAdmin.Domain.Repositories;
using UniversalAdmin.Infrastructure.Data.Contexts;

namespace UniversalAdmin.Infrastructure.Repositories;

public class DocumentRepository : Repository<Document>, IDocumentRepository
{
    public DocumentRepository(ApplicationDbContext context) : base(context)
    {
    }

    public async Task<Document?> GetByIdWithChunksAsync(Guid id)
    {
        return await _context.Documents
            .Include(d => d.Chunks)
            .FirstOrDefaultAsync(d => d.Id == id);
    }

    public async Task<IEnumerable<Document>> GetAllWithChunksAsync()
    {
        return await _context.Documents
            .Include(d => d.Chunks)
            .ToListAsync();
    }

    public async Task<IEnumerable<DocumentChunk>> GetDocumentChunksByDocumentIdAsync(Guid documentId)
    {
        return await _context.DocumentChunks
            .Where(dc => dc.DocumentId == documentId)
            .ToListAsync();
    }

    public async Task<IEnumerable<DocumentChunk>> GetAllDocumentChunksAsync()
    {
        return await _context.DocumentChunks.ToListAsync();
    }

    public IQueryable<Document> GetQueryable()
    {
        return _context.Documents.AsQueryable();
    }
}